#include "dma.h" 

uint32_t * M2M_SendData = null;
uint32_t * M2M_ReceiveData = null;

uint8_t * USART1_TX_DMA_data = null;
uint8_t * USART1_RX_DMA_data = null;

uint32_t periph[SRAM2SRAM_BUFFERSIZE] = 
{
    0,0x05060708,0x090A0B0C,0x0D0E0F10,
    0x11121314,0x15161718,0x191A1B1C,0x1D1E1F20,
    0x21222324,0x25262728,0x292A2B2C,0x2D2E2F30,
    0x31323334,0x35363738,0x393A3B3C,0x3D3E3F40,
    0x41424344,0x45464748,0x494A4B4C,0x4D4E4F50,
    0x51525354,0x55565758,0x595A5B5C,0x5D5E5F60,
    0x61626364,0x65666768,0x696A6B6C,0x6D6E6F70,
    0x71727374,0x75767778,0x797A7B7C,0x7D7E7F80
};
uint32_t sramdata[SRAM2SRAM_BUFFERSIZE] = {0};

/*基于dma的存储数据交互*/
void DMA_Init_sram2sram(void)
{
    DMA_InitTypeDef DMAInitStruct; 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    DMAInitStruct.DMA_PeripheralBaseAddr = (uint32_t)M2M_SendData;//
    DMAInitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;////数据宽度需要一致
    DMAInitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Enable;//外设寄存器指针动

    DMAInitStruct.DMA_MemoryBaseAddr = (uint32_t)(M2M_ReceiveData);//变量地址
    DMAInitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//数据宽度需要一致
    DMAInitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;

    DMAInitStruct.DMA_Priority = DMA_Priority_High;
    DMAInitStruct.DMA_BufferSize = SRAM2SRAM_BUFFERSIZE;
    DMAInitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;//外设作为源
    DMAInitStruct.DMA_Mode = DMA_Mode_Circular;//一次或者循环
    DMAInitStruct.DMA_M2M = DMA_M2M_Enable;//M2M

    DMA_Init(SRAM2SRAM_DMA_CHANNEL,&DMAInitStruct);
    DMA_Cmd (SRAM2SRAM_DMA_CHANNEL,ENABLE);

}

/*利用dma发送串口数据*/
void DMA_Init_sram2usart1(void)
{
    DMA_InitTypeDef DMAInitStruct; 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    DMAInitStruct.DMA_PeripheralBaseAddr = (uint32_t)USART_DR_ADDRESS;//usart dr寄存器地址
    DMAInitStruct.DMA_MemoryBaseAddr = (u32)(USART1_TX_DMA_data);//变量地址
    DMAInitStruct.DMA_DIR = DMA_DIR_PeripheralDST;//外设作为
    
    DMAInitStruct.DMA_BufferSize = DMA_DATA_PER_SECOND_USART1_TX;

    DMAInitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设寄存器指针不动
    DMAInitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;////数据宽度需要一致
    DMAInitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; 
    DMAInitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//数据宽度需要一致

    DMAInitStruct.DMA_Priority = DMA_Priority_High;
    DMAInitStruct.DMA_Mode = DMA_Mode_Circular;//一次或者循环
    DMAInitStruct.DMA_M2M = DMA_M2M_Disable;//关闭M2M

    DMA_Init(USART1TX_DMA_CHANNEL,&DMAInitStruct);
    // DMA_ClearFlag(DMA1_FLAG_TC4);
    DMA_Cmd (USART1TX_DMA_CHANNEL,ENABLE); 
}


/*利用dma接收数据*/
void DMA_Init_usart12sram(void)
{
    DMA_InitTypeDef DMAInitStruct; 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    DMAInitStruct.DMA_PeripheralBaseAddr = (uint32_t)USART_DR_ADDRESS;//usart dr寄存器地址
    DMAInitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;////数据宽度需要一致
    DMAInitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设寄存器指针不动

    DMAInitStruct.DMA_MemoryBaseAddr = (uint32_t)(USART1_RX_DMA_data);//变量地址
    DMAInitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//数据宽度需要一致
    DMAInitStruct.DMA_PeripheralInc = DMA_MemoryInc_Enable;

    DMAInitStruct.DMA_Priority = DMA_Priority_High;
    DMAInitStruct.DMA_BufferSize = DMA_DATA_PER_SECOND_USART1_RX;
    DMAInitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;//外设作为源
    DMAInitStruct.DMA_Mode = DMA_Mode_Circular;//一次或者循环
    DMAInitStruct.DMA_M2M = DMA_M2M_Disable;//关闭M2M

    DMA_Init(USART1RX_DMA_CHANNEL,&DMAInitStruct);
    DMA_Cmd (USART1RX_DMA_CHANNEL,ENABLE); 
}
